<template>
  <!-- 今年流通量 -->
  <div class="pie-chart">
    <div class="chart_pie_box" ref="chart_pie_box" />
  </div>
</template>

<script>
import { GaugeChart, PieChart } from "echarts/charts";
import { LegendComponent, ToolboxComponent } from "echarts/components";
import * as echarts from "echarts/core";
import { LabelLayout } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([
  ToolboxComponent,
  LegendComponent,
  PieChart,
  GaugeChart,
  CanvasRenderer,
  LabelLayout,
]);

export default {
  name: "lineChart",
  props: {
    chartData: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  data() {
    return {};
  },
  watch: {
    chartData: {
      handler(oldVal, newVal) {
        // 在 chartData 变化时重新构建图表 options
        console.log(oldVal, newVal, 404040);
        if (oldVal !== newVal) {
          this.initChart();
        }
      },
    },
  },
  methods: {
    createOption(data) {
      console.log(data,494949)
      var scale = 1;

      const colorLine = [
        "#00CEFF",
        "#00EBC6",
        "#BA16FF",
        "#C35617",
        "#413BFF",
        "#82E200",
        "#E0C752",
        "#6652E1",
        "#0DD5FB",
        "#0495B1",
        "#ca26ff",
        "#009D33",
      ];
      const colorLegend = [
        "#00D5FF",
        "#00E39D",
        "#BB15FF",
        "#C35616",
        "#0427FF",
        "#98F300",
        "#D4B538",
        "#3E45DF",
        "#92EAFB",
        "#79F8B6",
        "#ca26ff",
        "#009D33",
      ];
      var rich = {
        num: {
          color: "#fff",
          fontSize: 18 * scale,
          fontWeight: 600,
          padding: [5, 4],
          fontFamily: "Arial",
          align: "center",
        },
        total: {
          color: "#cecece",
          fontSize: 14 * scale,
          align: "center",
        },
        gary: {
          color: "#cecece",
          align: "center",
          fontSize: 14 * scale,
        },
        blue: {
          color: "#49dff0",
          fontSize: 14 * scale,
          align: "center",
        },
      };

      return {
        color: colorLine,
        legend: {
          orient: "vertical", //设置图例的方向
          top: '65%',
            y:'center',
          itemHeight: 10,
          itemWidth: 12,
          itemGap: 5, //设置图例的间距
          icon: "circle", // 图例项的icon,类型包括 circle(圆形),rect(正方形),
          //roundRect(圆角正方形),triangle(三角形),diamond(菱形),
          //pin(大头针行),arrow(箭头形),none(无图例项的icon)
          formatter: (e) => {
            let ratio = 0;
            let value = 0;
            //  通过e的内容,获取后台的百分比和车数
            if (data.length > 0) {
              data.forEach((item) => {
                if (e == item.name) {
                  ratio = ((item.value * 100) / 3799).toFixed(0);
                  value = item.value;
                }
              });
            }
            let arr = [
              "{a|" + e + "}",
              // "{b|" + ratio + "%}",
              "{c|" + value + "}" + "\n{d|}",
            ];
            // //此处的a,b,c是textStyle里面配置的a,b,c,d可以自定义
            return arr.join("");
          },
          textStyle: {
            //图例文字的样式
            rich: {
              d: {
                width: "100%",
                borderWidth: 0.5,
                height: 0,
                backgroundColor: "1",
              },
              a: {
                width: 90,
                align: "left",
                fontSize: 12,
                color: "#fff",
              },
              b: {
            width: 40,
            align: "left",
            fontSize: 12,
            color: "1",
          },
              c: {
                width: 30,
                align: "left",
                fontSize: 12,
                color: "1",
              },
            },
          },
        },
        series: [
          // 最内刻度线
          {
            type: "gauge",
            radius: "40%",
            center: ["50%", "37%"],
            clockwise: true,
            startAngle: "90",
            endAngle: "-269.9999",
            splitNumber: 90, //线的条数
            pointer: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            tooltip: {
              show: false,
            },
            splitLine: {
              show: true,
              length: 10,
              lineStyle: {
                color: "rgba(52, 185, 232, .5)",
                width: 1,
              },
            },
            axisLabel: {
              show: false,
            },
          },
          // 外圈背景
          {
            type: "pie",
            radius: ["25%", "51%"],
            center: ["50%", "37%"],

            hoverAnimation: false,
            clockWise: false,
            itemStyle: {
              normal: {
                borderWidth: 1,
                borderColor: "rgba(193, 229, 255, .1)",
                color: new echarts.graphic.LinearGradient(1, 1, 1, 0, [
                  {
                    offset: 1,
                    color: "rgba(127, 242, 255, .2)",
                  },
                  {
                    offset: 0,
                    color: "rgba(109, 195, 255, .1)",
                  },
                ]),
              },
            },
            tooltip: {
              show: false,
            },
            label: {
              show: false,
            },
            data: [100],
          },
          // 数据
          {
            type: "pie",
            radius: ["35%", "45%"],
            center: ["50%", "37%"],
            label: {
              show: false,
            },
            itemStyle: {
              normal: {
                // 设置渐变
                color: function (params) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 1,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: colorLine[params.dataIndex % colorLine.length],
                      },
                      {
                        offset: 1,
                        color:
                          colorLegend[params.dataIndex % colorLegend.length],
                        // 100% 处的颜色
                      },
                    ],
                    globalCoord: false, // 缺省为 false
                  };
                },
              },
            },
            data: data,
          },
        ],
      };
    },
    initChart() {
      if (this.chart) {
        this.chart.dispose();
      }
      if (!this.$refs.chart_pie_box) return;

      this.chart = echarts.init(this.$refs.chart_pie_box, "macaron");

      if (this.chartData && this.chartData.length) {
        let list = [];
        if (this.chartData.length < 2) {
          list = this.chartData
            .concat([
              { name: "自助借还1", value: 4 },
              { name: "自助借还2", value: 14 },
              { name: "自助借还3", value: 8 },
            ])
            .sort((a, b) => b.value - a.value);
        } else {
          list = this.chartData;
        }
        console.log(list,296296296296296)
        this.chart.setOption(this.createOption(list));
      }
    },
  },

  mounted() {
    this.initChart();
  },
};
</script>

<style lang="less" scoped>
.pie-chart {
  width: 100%;
  height: 100%;
  text-align: center;
  display: flex;
  align-items: start;
  justify-content: center;
  .chart_pie_box {
    width: 100%;
  height: 90%;
   
  }
}
</style>
